<?php
namespace jy_area9fan;
defined('BY_JYA') or exit('error');
/*
	分销插件会员类
	如果将分销商各项数据在每次数据变动时计算好并存入数据库。
	那么分别需要做
		在设置上下级关系时，修改新上级的 下线数量、分销商数量、订单数量、订单佣金数量等数据
		在变更上下级关系时，修改原、新上级的各项参数
		在发生订单时，修改上级的订单相关参数
		在用户成为分销商时，修改上级的分销商数量相关数据
*/
class member extends \plugin{
	
	/*
		会员注册
	*/
	public function register($member=array()){
		if(!$member['uniacid']){
			$member['uniacid']=$_SESSION['uniacid'];
		}
		if(!$member['mid']){
			$member['mid']=$_SESSION['uid'];
		}
		if(!$member['createtime']){
			$member['createtime']=TIMESTAMP;
		}
		if(!$member['mid']){
			return error('区域代理会员注册失败，缺少会员ID！');
		}
		$ismid=$this->get('member',array('mid'=>$member['mid']),array('mid'));
		if($ismid){
			return false;
		}
		$this->insert('member',$member);
	}
	/*
		申请成为区域代理
	*/
	public function apply_agent($mid,$area_code,$post=array()){
		if(!$area_code && !$post['is_duodai']){
			exi('获取区域code失败！','error');
		}
		$check=$this->getcolumn('agent_area',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'area_code'=>$area_code),'id');
		if($check){
			exi('您已经代理了该地区，请勿重复代理！','error');
		}
		if($this->setting['agent_type']==1){
			//独家代理模式下，不允许重复
			$check=$this->getcolumn('agent_area',array('uniacid'=>$_SESSION['uniacid'],'area_code'=>$area_code),'id');
			if($check){
				exi('该区域已存在代理！请更换！','error');
			}
		}
		if($this->setting['agent_many']==0){
			//不支持代理多个地区的情况下，此处检查
			$agent=$this->getcolumn('agent_area',array('mid'=>$mid),'id');
			if($agent){
				exi('您已经是代理商了，不能重复申请！','error');
			}
		}
		$new_apply=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'status'=>0,
			'area_type'=>pdo_getcolumn('core_area',array('code'=>$area_code),'type'),
			'area_code'=>$area_code,
			'area'=>ijson_encode($post['area'],true),
			'level_id'=>$post['level_type'],
			'createtime'=>TIMESTAMP,
		);
		$this->insert('agent_area',$new_apply);
		$id=$this->insertid();
		return $id;
	}
	public function up_agent($mid,$area_code,$post=array()){
		if(!$area_code){
			exi('请选择区域省份！','error');
		}
		$new_apply=array();
		if($area_code){
			$new_apply['area_code']	=$area_code;
			$new_apply['area_type']	=pdo_getcolumn('core_area',array('code'=>$area_code),'type');
			$new_apply['area']=ijson_encode($post['area']);
		}
		$this->update('agent_area',$new_apply,array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['id']));
		return $id;
	}
	/*
		审核区域代理
	*/
	public function check_agent($agent_id,$status,$remark=''){
		if(!$agent_id || !$status){
			return;
		}
		$agent_area=$this->get('agent_area',array('id'=>$agent_id));
		if(!$agent_area){
			return;
		}
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
		);
		if($update['status']==1){
			//审核通过，如果会员还不是代理商，此处修改
			$this->update('member',array('is_agent'=>1),array('mid'=>$agent_area['mid'],'is_agent'=>0));
		}else{
			$update['remark']=$remark;
		}
		$this->update('agent_area',$update,array('id'=>$agent_id));
		if($update['status']==1){
			$this->model('notice')->notice_fenxiao_add($agent_area['mid']);
		}
		return true;
	}
	public function set_parent($mid,$fromuser){
		$this->update('member',array('agent_id'=>$fromuser),array('mid'=>$mid));
		$this->model('notice')->notice_fenxiao_xiajiadd($mid,$fromuser);
		return true;
	}
	/*
		禁用代理商
	*/
	public function stop_agent($agent_id){
		$this->update('member',array('stop'=>1),array('mid'=>$agent_id));
	}
	/*
		恢复代理商
	*/
	public function start_agent($agent_id){
		$this->update('member',array('stop'=>0),array('mid'=>$agent_id));
	}
	//直接开通等级
	public function up_arealevel($mid,$level_id=0){
		$agent_area=$this->get('agent_area',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		if($agent_area['id']){
			if($level_id>$agent_area['level_id']){
				$this->update('agent_area',array('level_id'=>$level_id),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
			}
		}else{
			$new_apply=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$mid,
				'status'=>1,
				'level_id'=>$level_id,
				'createtime'=>TIMESTAMP,
			);
			$this->insert('agent_area',$new_apply);
			$id=$this->insertid();
		}	
		return $id;
	}
	/*
		升级分销商等级
		先检测会员身份是否为正常分销商
		然后获取分销商当前等级的下一等级升级条件
		然后逐个条件对比，看是否满足升级条件
		如果全部满足，则升级到相应等级
		
		何时触发升级分销商等级检测呢？
		
	*/
	public function update_agent_level($agent_id){
		$member=$this->get('agent_area',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$agent_id));
		if(!$member['mid']){
			return;
		}
		$fenset=$this->setting['fenset'];
		paixu($fenset,'key','desc');
		$up=false;
		foreach($fenset as $lv){
			if($lv['goods'] && $lv['num']){
				$goods_ids=json_decode($lv['goods'],true);
				foreach($goods_ids as $gv){
					$isbuynum=pdo_getsum('jy_weishop_order_goods',array('ispay'=>1,'mid'=>$agent_id,'goodsid'=>$gv['id']),'total');
					if($isbuynum>0 && $isbuynum>$lv['num']){
						$up=true;
						break;
					}
				}
			}else{
				$up=true;
			}
			if($up){
				$next_level=$lv;
				break;
			}
		}
		if($up){
			$update=array(
				'level_id'=>$next_level['key'],
			);
			$this->update('agent_area',$update,array('uniacid'=>$_SESSION['uniacid'],'mid'=>$agent_id));
		}
		return true;
	}
	public function getteamids($teammid,$nomy=false){
		$newmembers=array();
		$newteammids=$teammid;
		for($i=1;$i<9;$i++){
		    $params=array('uniacid'=>$_SESSION['uniacid'],'fromuser'=>$newteammids);
			$members=pdo_getall('core_members',$params,array('id'),'id');
			if($members){
				foreach($members as $key=>$item){
					$newmembers[$key]=$key;
				}
				$newteammids=array_keys($members);
			}else{
				break;
			}
		}
		if($newmembers){
			$teamids=array_keys($newmembers);
		}else{
			$teamids=$nomy?array():array($teammid);
		}
		return $teamids;
	}
}